'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var moment = require('moment');
var _ = require('lodash');
var Code = require('../../../consts/code');
var moduleType = require('../../../consts/moduleType');
var operationType = require('../../../consts/operationType');
var PlayerManager = require('../../../libs/playerManager');
var dataApi = require('../../../libs/dataApi');
var rewardService = require('../../../services/rewardService');
var roleService = require('../../../services/roleService');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

/**
 * 领取每日体力
 */
Handler.prototype.receive = function (msg, session, next) {
    var role_id = session.uid;
    var task_id = msg.task_id;
    var reward_list = [];
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [

            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'bag', 'dailyEnergy', 'vip', 'build', 'task', 'daily_task', 'achieve', 'tech', 'sevenDayActivity', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var daily_energy_model = player_manager.dailyEnergy;
                var role_model = player_manager.role;
                reward_list = dataApi.get_sp.findById(task_id);

                // 是否在领取时间
                var total_delay_time = reward_list.finish_time + 300;
                var time_poor = moment().startOf('day').add(reward_list.start_time, 's') <= moment() && moment().startOf('day').add(total_delay_time, 's') >= moment();
                if (!time_poor) {
                    return next(null, {
                        code: Code.TASK_DAILY_TIME_ENERGY
                    });
                }

                // 是否领取过
                var state = daily_energy_model.addTimeList(task_id);
                if (!state) {
                    return next(null, {
                        code: Code.TASK_ENERGY_NOT_RECEICE
                    });
                } else {
                    // 发放体力
                    rewardService.addRewardListToRole(player_manager, [reward_list], moduleType.MDL_ACT_DAILY_ENERGY, operationType.OPR_ACT_DAILY_ENERGY);
                }
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    reward_list: [reward_list]
                }
            });
        }
    );
};

/**
 * 补签体力
 */
Handler.prototype.retroactive = function (msg, session, next) {
    var role_id = session.uid;
    var task_id = msg.task_id;
    var reward_list = [];
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [

            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'bag', 'dailyEnergy', 'vip', 'build', 'soldier', 'task', 'daily_task', 'achieve', 'tech', 'sevenDayActivity', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var daily_energy_model = player_manager.dailyEnergy;
                var role_model = player_manager.role;
                reward_list = dataApi.get_sp.findById(task_id);

                // 是否领取过
                var state = daily_energy_model.addTimeList(task_id);
                if (!state) {
                    return next(null, {
                        code: Code.TASK_ENERGY_NOT_RECEICE
                    });
                } else {
                    // 扣除钻石
                    var sub_diamond = reward_list.point_cost;
                    if (!roleService.subDiamond(player_manager, moduleType.MDL_OTHER, operationType.OPR_OTHER_VIP_BOX, sub_diamond)) {
                        return next(null, {
                            code: Code.USER_DIAMOND_NOT_ENOUGH
                        });
                    }

                    // 发放体力
                    rewardService.addRewardListToRole(player_manager, [reward_list], moduleType.MDL_ACT_DAILY_ENERGY, operationType.OPR_ACT_DAILY_ENERGY);
                }
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    reward_list: [reward_list]
                }
            });
        }
    );
};
